class Parameter < ActiveRecord::Base
  set_table_name :product_parameters

  has_many :parameter_values, :dependent => :destroy

  has_one  :explanation_note, :as => :explanation, :dependent => :destroy

  has_and_belongs_to_many :products, :join_table => "product_has_parameters"

  accepts_nested_attributes_for :parameter_values, :allow_destroy => true, :reject_if => lambda { |attr| attr['value'].blank? && attr['presentation'].blank? }
  accepts_nested_attributes_for :explanation_note,  :allow_destroy => true, :reject_if => :all_blank

  validates :name, :presentation, :length => {:maximum => 40}, :presence => true

  has_paper_trail
  diff :exclude => [:id]

  #delegate :note, :note=, :type, :type=, :to => "explanation_note", :prefix => true
  delegate :note, :to => "explanation_note"

  alias new_explanation_note explanation_note
  def explanation_note
    new_explanation_note || build_explanation_note
  end

  def deletable?
    products.blank?
  end

  def self.values_to_sentence
    self.parameter_values.map(&:value).to_sentence
  end
end
